package Window;
/*
 * A View osztály hozza létre a megjelenítés alapjául szolgáló főablakot, 
 * valamint a főablakot felépítő osztályok példányait.
 * */
import java.awt.*;
import java.io.File;

import javax.swing.*;

import ModelAndController.Controller;

public class View extends JFrame{
	
	//A controller referenciaja
	Controller controller;
	
	//A foablak valtozoja
	private JFrame frame;
	//A foablakok elemeit tartalmazo container
	private Container container;
	//A foablak elemeinek elhelyezesere szolgalo verticalBox
	private Box verticalBox;
	//Az ablak merete az adott kepernyon
	private Dimension windowSize;
	
	//A halozat letrehozasaert felelos panel
	private CreateCircuitPanel createCircuitPanel;
	private File dirOfLastUsedFolder;
	private String fileName;
	//A halozat utemezeseert felelos panel
	private StepPanel stepPanel;
	//A kapcsolok es jelgeneratorok hozzafereset biztosito panel
	private InputPanel inputPanel;
	//A kijelzo es oszcilloszkop hozzafereset biztosito panel
	private OutputPanel outputPanel;
	
	//A halozat betoltettseget jelzo boolean
	private boolean isCircuitLoaded;
	//A kapcsolo ertekei
	private int[] switchValues;
	//A kijelzo ertekei
	private int[] displayValues;
	//A jelgenerator aktualis ertekei
	private int[] signalValues;
	//A jelgeneratorok ciklikusan ismetlodo ertekeit tartalmazza
	private int[][] allSignalValues;
	//Az oszcilloszkop stabil ertekeit tarolo tomb
	private int[][] stableScopeValues;
	//Az oszcilloszkop delta ertekeit tartalmazza
	private int[][] temporalScopeValues;
	
	//Konstruktor
	public View(Controller controller){
		this.controller = controller;
		this.isCircuitLoaded = false;
		fileName = "";
		
		frame = new JFrame("Circuit Handler "+fileName);
		container = frame.getContentPane();
		//kilépés ha bezárjuk a főablakot
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		getWindowSize();
		frame.setSize(windowSize);
		frame.setVisible(true);
		regenerateWindow();
	}

	//Beallitja az ablak meretet
	private void getWindowSize(){
		Toolkit tk = Toolkit.getDefaultToolkit();
		windowSize = tk.getScreenSize();
	}
	
	//Ujrageneralja az ablakot
	public void regenerateWindow(){
		frame.setTitle("Circuit Handler: "+fileName);
		//Tartalom torlese
		container.removeAll();
		verticalBox = Box.createVerticalBox();
		
		//CreateCicuitPanel peldany
		createCircuitPanel = new CreateCircuitPanel(this, controller, windowSize, dirOfLastUsedFolder);
		verticalBox.add(createCircuitPanel.getPanel());
		
		//StepPanel peldany
		stepPanel = new StepPanel(this, windowSize, controller, isCircuitLoaded);
		verticalBox.add(stepPanel.getPanel());
		
		verticalBox.add(Box.createVerticalGlue());
		
		//Input es OutputPanel
		Box horizontalOfInputOutputPanels = Box.createHorizontalBox();
		inputPanel = new InputPanel(this, windowSize);
		outputPanel = new OutputPanel(this, windowSize);
		horizontalOfInputOutputPanels.add(inputPanel.getPanel());
		horizontalOfInputOutputPanels.add(outputPanel.getPanel());
		verticalBox.add(horizontalOfInputOutputPanels);
		
		verticalBox.add(Box.createVerticalGlue());
		
		container.add(verticalBox);
		frame.setContentPane(container);
	}
	
	//Az elkapott exception tartalmat irja ki
	public void catchException(Exception e){
		JOptionPane.showMessageDialog(frame, e.getMessage(), "Warning", JOptionPane.NO_OPTION);
	}
	
	//Minden erteket defaultra allit
	public void resetAllValues(){
		//isCircuitLoaded false ertekre
		isCircuitLoaded = false;
		//switchValues null
		switchValues = null;
		//displayValues null
		displayValues = null;
		//signalValues null
		signalValues = null;
		//allSignalValues null
		allSignalValues = null;
		//stableScopeValues null
		stableScopeValues = null;
		//temporalScopeValeus null
		temporalScopeValues = null;
	}
	
	//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*//*
	//Set-Get methodok
	
	//Visszaadja a controller referenciajat
	public Controller getController(){
		return controller;
	}
	
	//Visszaadja az ablak JFrame-jet
	public JFrame getFrame(){
		return frame;
	}
	
	//Visszaadja az inputPanel 
	public InputPanel getInputPanel(){
		return inputPanel;
	}
	
	//fileName beallitasa
	public void setFileName(String copy){
		fileName = copy;
	}
	
	//dirOfLastUsedFolder beallitasa
	public void setDirOfLastUsedFolder( File dir){
		dirOfLastUsedFolder = dir;
	}
	
	//isCircuitLoaded beallitasa
	public void setIsCircuitLoaded(boolean value){
		isCircuitLoaded = value;
	}
	
	//visszaadja az isCircuitLoaded erteket
	public boolean getIsCircuitLoaded(){
		return isCircuitLoaded;
	}
	
	//switchValues beallitasa
	public void setSwitches(int[] values){
		if(values != null)
			switchValues = values.clone();
	}
	
	//visszaadja a switchValues
	public int[] getSwitches(){
		return switchValues;
	}
	
	//displayValues beallitasa
	public void setDisplay(int[] values){
		if(values != null)
			displayValues = values.clone();
	}
	
	//visszaadja a displayValues
	public int[] getDisplay(){
		return displayValues;
	}
	
	//signalValues beallitasa
	public void setSignal(int[] values){
		if(values != null)
			signalValues = values.clone();
	}
	
	//visszaadja a signalValues
	public int[] getSignal(){
		return signalValues;
	}
	
	//allSignalValues beallitasa
	public void setAllValuesOfSignal(int[][] values){
		if(values != null)
			allSignalValues = values.clone();
	}
	
	//visszaadja az allSignalValues
	public int[][] getAllValuesOfSignal(){
		return allSignalValues;
	}
	
	//stableScopeValues beallitasa
	public void setStableScopeValues(int[][] values){
		if(values != null)
			stableScopeValues = values.clone();
	}
	
	//visszaadja a stableScopeValues
	public int[][] getStableScopeValues(){
		return stableScopeValues;
	}

	//temporalScopeValues beallitasa
	public void setTemporalScopeValues(int[][] values){
		if(values != null)
			temporalScopeValues = values.clone();
	}
	
	//visszaadja a getTemporalScopeValues
	public int[][] getTemporalScopeValues(){
		return temporalScopeValues;
	}
	
}
